package com.csot.ry.util;

import com.csot.common.core.domain.BaseEntity;
import com.csot.common.core.domain.SegmentEntity;

import java.util.List;

/**
 * @author lenovo
 * @create 2022/6/2 14:12
 */
public class SegmentUtil {
//    //验证传进来的数据是否有重复
    public static boolean checkTotalSegmentIsRepeat(List<? extends BaseEntity> totalSegmentList) {
        boolean b = true;

        for (int i = 0 ;i<totalSegmentList.size()-1;i++){
        SegmentEntity t_i = (SegmentEntity) totalSegmentList.get(i);
            for (int j = i+1;j<totalSegmentList.size();j++){
                SegmentEntity t_j = (SegmentEntity) totalSegmentList.get(j);
                if((t_i.getIpStartI()>=t_j.getIpStartI()
                        && t_i.getIpStartI() <= t_j.getIpEndI())
                        || (t_i.getIpEndI() >=t_j.getIpStartI()
                        && t_i.getIpEndI() <=t_j.getIpEndI())
                ){
                    b = false;
                    break;
                }
            }
            if(!b){
                break;
            }
        }
        return false;
    }
//
//        AtomicBoolean resultb = new AtomicBoolean(true);
//        TTotalSegment tTotalSegment_i = null;
//        TTotalSegment tTotalSegment_j = null;
//            for (int i = 0 ;i<totalSegmentList.size()-1;i++){
//                tTotalSegment_i = (classSegment)totalSegmentList.get(i);
//                for (int j = i+1;j<totalSegmentList.size();j++){
//                    tTotalSegment_j = totalSegmentList.get(j);
//                    if((tTotalSegment_i.getIpStartI()>=tTotalSegment_j.getIpStartI()
//                            && tTotalSegment_i.getIpStartI() <= tTotalSegment_j.getIpEndI())
//                            || (tTotalSegment_i.getIpEndI() >=tTotalSegment_j.getIpStartI()
//                            && tTotalSegment_i.getIpEndI() <=tTotalSegment_j.getIpEndI())
//                    ){
//                        resultb.set(false);
//                        break;
//                    }
//                }
//                if(!resultb.get()){
//                    break;
//                }
//            }
//        return resultb.get();
//    }
//
//    //处理X.X.X.X/X
//    //设置转换IP为整形
//    private List<TTotalSegment> getTotalSegmentList(List<TTotalSegment> tTotalSegmentList){
//        List<TTotalSegment> rList  = tTotalSegmentList;
//        rList.stream().parallel().forEach(ts ->{
//            if(ts.getIpStart().contains("/") && StrUtil.isEmpty(ts.getIpEnd()) ){
//                List<String> ipList = Ipv4Util.list(ts.getIpStart(), true);
//                ts.setIpStart(ipList.get(0));
//                ts.setIpStartI(NetUtil.ipv4ToLong(ipList.get(0)));
//                ts.setIpEnd(ipList.get(ipList.size()-1));
//                ts.setIpEndI(NetUtil.ipv4ToLong(ipList.get(ipList.size()-1)));
//            }else{
//                ts.setIpStartI(NetUtil.ipv4ToLong(ts.getIpStart()));
//                ts.setIpEndI(NetUtil.ipv4ToLong(ts.getIpEnd()));
//            }
//        });
//        return rList;
//    }
}
